package com.xtck.ha.fault.core.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xtck.ha.common.core.domain.R;
import com.xtck.ha.common.core.web.domain.AjaxResult;
import com.xtck.ha.common.core.web.service.dsimpl.BaseServiceImpl;
import com.xtck.ha.common.core.config.DsServerConfig;
import com.xtck.ha.common.core.utils.DomainUtils;
import com.xtck.ha.fault.core.model.DsModInfo;
import com.xtck.ha.fault.core.service.DsModInfoService;
import com.xtck.ha.fault.framework.utils.DoshellUtils;
import com.xtck.ha.fault.framework.utils.FileInfosUtils;
import com.xtck.ha.fault.framework.utils.GetPidUtil;
import com.xtck.ha.fault.framework.utils.JsonResult;
import com.xtck.ha.fault.modules.convert.ModInfoConvert;
import com.xtck.ha.fault.modules.pojo.entity.ModInfo;
import com.xtck.ha.fault.modules.pojo.vo.ModInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @Description: DsModInfoService实现类
 * @Auther:zhoutanxin
 * @Date:2025-07-01
 */
@Slf4j
@Service
public class DsModInfoServiceImpl extends BaseServiceImpl<DsModInfo> implements DsModInfoService {
  /**
  *
  * 冗余代码，为自定义开发DS-Server接口服务
  *
  */
   private static String TABLE_NAME = "MOD_INFO";
   private String getUrl(){
      return getDsServerConfig().getUrl();
   }
    /**
     * 启动模型服务
     * @param modInfo 模型信息
     * @return 结果日志
     */
    @Override
    public AjaxResult startAlgorithmServer(DsModInfo modInfo){
        DsModInfo entity = this.selectTById(modInfo.getId());
        List<String> paramlist = new ArrayList<>();
        paramlist.add(entity.getModPath() + "/" + entity.getStartServiceFile());
        DoshellUtils.onCall("python",paramlist);
        entity.setServerState(true);
        this.updateT(entity);
        return AjaxResult.success("模型服务启动");
    }

    /**
     * 停止模型服务
     * @param modInfo 模型实体
     * @return 结果日志
     */
    @Override
    public AjaxResult stopAlgorithmServer(DsModInfo modInfo){
        DsModInfo entity = this.selectTById(modInfo.getId());
        String closeResult = "";
        try {
            closeResult = GetPidUtil.stopByPid(entity.getModPath() + "/" + entity.getStartServiceFile());
            entity.setServerState(false);
            updateT(entity);
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("模型服务停止,"+closeResult);
        return AjaxResult.success("模型服务停止,"+closeResult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(DsModInfo dsModInfo, MultipartFile algorithmFile) {
        /**检查模型名称是否重复*/
        List<DsModInfo> list = selectTList(DsModInfo.builder().modName(dsModInfo.getModName()).build());
        if (null != list && list.size() != 0)
            return AjaxResult.error("模型名称不能重复！");
        else {
            // 上传文件
            if (BeanUtil.isEmpty(algorithmFile)) {
                return AjaxResult.error("模型文件不能为空");
            }
            String filePath = null;
            try {
                if (algorithmFile.getOriginalFilename().endsWith("zip"))
                    filePath = FileInfosUtils.uploadZip(algorithmFile);
                else
                    filePath = FileInfosUtils.uploadFile(algorithmFile);
            } catch (Exception e) {
                return AjaxResult.error(e.getMessage());
            }
            dsModInfo.setModPath(filePath);
            dsModInfo.setServerState(false);
            dsModInfo.setCreateTime(new Date());
            insertT(dsModInfo);
            return AjaxResult.success("模型新建成功");
        }
    }
}
